package com.wtgroup.demo.mianshi.算法.字符串.字符串匹配;

import com.wtgroup.demo.common.util.RandomUtil;
import org.openjdk.jmh.annotations.*;

/**
 * @author dafei
 * @version 0.1
 * @date 2021/4/8 20:38
 */
public class PTestKMP {
    /**
     * Benchmark             Mode  Cnt  Score   Error  Units
     * PTestKMP.testKmp     thrpt    3  0.863 ± 1.314  ops/s
     * PTestKMP.testSimple  thrpt    3  1.212 ± 0.649  ops/s
     */

    static String[] strs;
    static String[] ms;
    static {
        int len = 100_0000;
        strs = RandomUtil.randomArrayString(len, 1000, 8907);
        ms = RandomUtil.randomArrayString(len, 100, 8907);
    }

    @Benchmark
    @Warmup(iterations = 0, time = 1)
    @Fork(1) // 多少个线程执行
    @BenchmarkMode(Mode.Throughput) // 模式
    @Measurement(iterations = 3)
    public void testKmp() {
        for (int i = 0; i < strs.length; i++) {
            KMP.kmp(strs[i], ms[i]);
        }
    }

    @Benchmark
    @Warmup(iterations = 0, time = 1)
    @Fork(1) // 多少个线程执行
    @BenchmarkMode(Mode.Throughput) // 模式
    @Measurement(iterations = 3)
    public void testSimple() {
        for (int i = 0; i < strs.length; i++) {
            SimpleStrMatcher.search(strs[i], ms[i]);
        }
    }

}
